package com.txhy.certificatechain.utils;

import com.google.gson.Gson;
import com.txhy.certificatechain.model.data.ResponseData;
import com.txhy.certificatechain.model.data.ResponseStringData;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: GsonUtil.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: JSON解析类
 * 创建时间: 2018/9/19 15:07
 * 更新时间：2018/9/19 15:07
 * -----------------------------------------------------------------
 * 2018/9/19 15:07: 创建 GsonUtil.java (侯建军);
 * -----------------------------------------------------------------
 */
public class GsonUtil {
    private static Gson gson = null;
    static {
        if (gson == null) {
            gson = new Gson();
        }
    }

    private GsonUtil() {
    }


    /**
     * 转成bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }

    /**
     * 获取Status
     * @param gsonString
     * @return
     */
    public static int GsonGetStatus(String gsonString) {
        int status = 0;
        if (gson != null) {
            status=gson.fromJson(gsonString, ResponseData.class).getStatus();
        }
        return status;
    }

    /**
     * 获取msg
     * @param gsonString
     * @return
     */
    public static String GsonGetMsg(String gsonString) {
        String msg = null;
        if (gson != null) {
            msg=gson.fromJson(gsonString, ResponseData.class).getMsg();
        }
        return msg;
    }

    /**
     * 获取data
     * @param gsonString
     * @return
     */
    public static String GsonGetData(String gsonString) {
        String msg = null;
        if (gson != null) {
            msg=gson.fromJson(gsonString, ResponseStringData.class).getData().toString();
        }
        return msg;
    }
}

